<section class="content">
	<div class="row">
		<div class="col-md-3">
			<div class="box box-primary">
				<h4 class="profile-username text-center">生产流程管理</h4>
				<p class="text-muted text-center">请选择产品</p>
				<div class="box-body no-padding">
					<ul id="prodList" class="nav nav-pills nav-stacked">
					</ul>
				</div>
				<!-- /.box-body -->
			</div>
			<div id="batchDiv">
				<!-- <div class="box box-solid">
					<div class="box-header with-border">
					
						<h3 class="box-title">批次1</h3>
						<div class="box-tools">
							<button type="button" class="btn btn-box-tool" data-widget="collapse"> <i class="fa fa-minus"></i> </button>
						</div>
					</div>
					
					<div class="box-body no-padding" style="display: block;">
						<ul class="nav nav-pills nav-stacked">
							<li><a href="#"><i class="fa fa-circle-o text-red"></i> 播种<span class="fa fa-long-arrow-down pull-right"></span></a></li>
							<li><a href="#"><i class="fa fa-circle-o text-yellow"></i> 浇水 <span class="fa fa-long-arrow-down pull-right"></span></a></li>
							<li><a href="#"><i class="fa fa-circle-o text-light-blue"></i> 施肥<span class="fa fa-long-arrow-down pull-right"></span></a></li>
						</ul>
					</div>
					/.box-body
				</div> -->
				
			</div>
			<!-- /. box -->

			<!-- /.box -->
		</div>
		<!-- /.col -->
		<div class="col-md-9">
			<div class="box box-primary">
				<div id="pnodePage" >
				</div>  
			</div>
			<!-- /. box -->
		</div>
		<!-- /.col -->
	</div>
	<!-- /.row -->
</section>

<script>
	var currentProd = '';
	var currentNodeId = '';
	$(function() {
		initPord();
		loadBatch(currentProd);
		loadNodeTable(currentNodeId);
	});

	function initPord() {
		var prods;
		var prodsHtml = '';
		ajaxPost(basePath + '/produce/getAll', null, function(data) {
			if (data && data.length > 0) {
				prods = data;
			}
		});

		for (var i = 0; i < prods.length; i++) {
			if (i == 0) {
				currentProd = prods[i].id;
			}
			prodsHtml += '<li><a href="#" onclick="loadBatch(\'' + prods[i].id + '\')">' + prods[i].name + '</a></li>';
		}
		$("#prodList").empty();
		$("#prodList").append(prodsHtml);
	}

	function loadBatch(proId) {
		currentProd = proId;
		var batchs;
		var batchHtml = '';
		ajaxPost(basePath + '/batch/getBatchByPorId/'+proId, null, function(data) {
			if (data && data.length > 0) {
				batchs = data;
			}
		});
		
		for(var i=0;i<batchs.length;i++){
			batchHtml += '<div class="box box-solid">';
			batchHtml += '    <div class="box-header with-border">';
			batchHtml += '        <h3 class="box-title">'+batchs[i].pcName+'</h3>';
			batchHtml += '        <div class="box-tools">';
			batchHtml += '            <button type="button" class="btn btn-box-tool" data-widget="collapse"> <i class="fa fa-minus"></i> </button>';
			batchHtml += '        </div>';
			batchHtml += '    </div>';
			batchHtml += '    <div class="box-body no-padding" style="display: block;">';
			batchHtml += '        <ul class="nav nav-pills nav-stacked">';
			
			var pnodes;
			ajaxPost(basePath + '/pnode/getPnodeByFlow/'+batchs[i].proName, null, function(result) {
				if (result && result.length > 0) {
					pnodes = result;
				}
			});
			for(var k=0;k<pnodes.length;k++){
				if(k == 0){
					currentNodeId = pnodes[k].id;
				}
				batchHtml += '            <li><a href="#" onclick="loadNodeTable(\''+pnodes[k].id+'\')"><i class="fa fa-circle-o text-light-blue"></i>'+pnodes[k].name+'<span class="fa fa-long-arrow-down pull-right"></span></a></li>';
			}
			batchHtml += '        </ul>';
			batchHtml += '    </div>';
			batchHtml += '</div>';
		}
		$("#batchDiv").empty();
		$("#batchDiv").append(batchHtml);
	}
	
	function loadNodeTable(pnodeId){
		currentNodeId = pnodeId;
		var container = "#pnodePage";
		var hasFiled = true;
		ajaxPost(basePath + '/dfiled/isFiledByNodeId?nodeId='+pnodeId, null, function(result) {
			hasFiled = result.success;
		});
		if(!hasFiled){
			jQuery(container).html("");
            modals.info("请先配置节点表单数据");
            return;
		}
	    var url = basePath+"/dform/list?nodeId="+pnodeId;
	    jQuery(container).load(url, function (response, status, xhr) {
	        if (status == "success") {
	            if (response) {
	                try {
	                    var result = jQuery.parseJSON(response);
	                    if (result.code != 100) {
	                        jQuery(container).html("");
	                        modals.info("请先配置节点表单数据");
	                    }
	                } catch (e) {
	                    return response;
	                }
	            }
	        }
	    });
	}
</script>
